package main

import (
	"fmt"
	"time"
)

/*
*
当channel存在时，发送和接收不需要相互阻塞
只有当cap满了后再发送的数据 才会导致阻塞
*/
func main() {
	//带有缓冲的channel
	c := make(chan int, 3)

	fmt.Println("len(c)=", len(c), ",cap(c)=", cap(c))

	go func() {
		defer fmt.Println("子go结束")

		for i := 0; i < 4; i++ {
			c <- i
			fmt.Println("子go程正在运行，发送元素=", i, "len(c)=", len(c), ",cap(c)=", cap(c))
		}
	}()

	time.Sleep(2 * time.Second)

	for i := 0; i < 4; i++ {
		//从c中接收数据
		num := <-c
		fmt.Println("num=", num)
	}

	time.Sleep(2 * time.Second)

	fmt.Println("main结束")
}
